"""
@Author: CheyenneLam
@Date: 2020-05-17 17:45:43
@LastEditTime: 2020-05-17 17:45:44
@LastEditors: CheyenneLam
@Description: 封装常规用方法
@FilePath: \Main\common.py
"""
import os
import logging

def GetRecentFile(path,key=None):
    """
    获取文件夹内有对应key的最新的文件
    """
    file_filter = filter(lambda x: key in x, os.listdir(path)) if key else os.listdir(path)
    file = sorted(file_filter, key = lambda x: os.path.getmtime(os.path.join(path,x)), reverse=True)[0]
    print(file)
    return os.path.join(path, file)

def zipDir(dirpath, outFullName, password=None):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    :param outFullName: 保存路径+xxxx.zip
    :return: 
    """
    if password:
        # 有密码时设置密码并压缩
        cmd = "zip -P %s -r %s %s" % (password, outFullName, dirpath)
    else:
        # 无密码直接压缩
        cmd = "zip -r %s %s" % (outFullName, dirpath)   
    status = os.popen(cmd)
    # 执行系统命令
    return outFullName

def StartLogging():
    """
    日志设置
    """
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    logFile = os.path.join(os.path.dirname(__file__),'record.log')
    logFile = 'record.log'
    formatter = logging.Formatter('%(asctime)s-%(levelname)s-%(message)s',datefmt='%Y/%m/%d %H:%M:%S')
    handler = logging.FileHandler(logFile)
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger